package com.ghline.caloncabe;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.anhthao.nuotcabe.R;
import com.ghline.caloncabe.manager.SoundManager;
import com.ghline.caloncabe.model.BitmapFont;
import com.ghline.caloncabe.model.Fish;
import com.ghline.caloncabe.model.LevelSelectSprite;
import com.ghline.caloncabe.model.MyProgressBar;
import com.ghline.caloncabe.model.MySprite;
import com.ghline.caloncabe.model.Sprite;
import com.ghline.caloncabe.model.SuaBien;
import com.ghline.caloncabe.model.TempLevelUp;
import com.ghline.caloncabe.model.TempSprite;

public class GameView extends SurfaceView {	
	
	private SurfaceHolder holder;
	private GameThread gameThread;	
	
	public static final int WIDTH = 800;
	public static final int HEIGHT = 480;
	
	public static final int SMALL_FISH_NUM = 11;
	public static final int MEDIUM_FISH_NUM = 6;
	public static final int BIG_FISH_NUM = 3;
	public static final int MAX_LIFE = 5;
	public static final int MIN_LIFE = 0;
	public static final String KEY_NUMBER_LEVEL = "level";
	public static final String KEY_MUSIC = "music";
	public static final String KEY_SOUND = "sound";	
	
	public static final float SOUND_SPEED = 1f;
	public static final int SOUND_EAT = 1;
	public static final int SOUND_DIE = 2;
	public static final int SOUND_LEVEL_UP = 3;
	public static final int SOUND_WIN = 4;
	public static final int SOUND_OVER = 5;
	public static final int MUSIC_BG = 6;
	
	private List<Sprite> spritesSmall = new ArrayList<Sprite>();	
	private List<Sprite> spritesMedium = new ArrayList<Sprite>();
	private List<Sprite> spritesBig = new ArrayList<Sprite>();	
	private List<Fish> fishes = new ArrayList<Fish>();
	private List<TempSprite> tempSprites = new ArrayList<TempSprite>();
	private List<TempLevelUp> tempLevelUps = new ArrayList<TempLevelUp>();
	private int moveEvent;
	private int score = 0;
	
	
	private Bitmap fishBitmapOpen;
	
	private Bitmap fishSmallBitmap;
	private Bitmap fishMediumBitmap;
	private Bitmap fishBigBitmap;
	
	private Bitmap spriteBigBitmap1;
	private Bitmap spriteBigBitmap2;
	private Bitmap spriteBigBitmap3;
	private Bitmap spriteBigBitmap4;	
	
	private Bitmap spriteMediumBitmap1;	
	private Bitmap spriteMediumBitmap2;	
	private Bitmap spriteMediumBitmap3;
	private Bitmap spriteMediumBitmap4;
	
	private Bitmap spriteSmallBitmap1;	
	private Bitmap spriteSmallBitmap2;
	private Bitmap spriteSmallBitmap3;
	private Bitmap spriteSmallBitmap4;	
	
	private Bitmap bmpTemp;
	private Bitmap bmpTempLevelUp;	
	private Bitmap scaledBackGround;
	private Bitmap mainMenuBackGround;
	private Bitmap scoreBarScaled;	
	private Fish fish;
	private Sprite spriteBig;
	private Sprite spriteMedium;
	private Sprite spriteSmall;
	private Bitmap progressBitmap;
	private Bitmap separate1;
	private int life;
	private MyProgressBar mProgressBar;
	private String scoreString;
	private String lifeString;
	private String levelString;
	private float scaleY;
	private float scaleX;
	private BitmapFont mBitmapFont; 
	private BitmapFont mBitmapFontLife;
	private BitmapFont mBitmapFontNumLevel;
	private int progress = 0;
	private int progressToWin = 0;
	private boolean enterGame = false;
	private boolean canMove = false;
	private int eventX;
	private int eventY;
	private Bitmap buttonPlayBitmap;
	private MySprite btPlay;
	private Bitmap buttonExitBitmap;
	private MySprite btExit;
	private Bitmap buttonHelpBitmap;
	private MySprite btHelp;
	private Bitmap buttonPauseBitmap;
	private Bitmap buttonUnPauseBitmap;
	private MySprite btPause;
	private Bitmap bg_menu_Bitmap;
	private MySprite bgMenu;
	private Bitmap logoPauseBitmap;
	private MySprite logoPause;
	private Bitmap buttonMenuBitmap;
	private MySprite btMenu;
	private Bitmap buttonResumeBitmap;
	private MySprite btResume;
	private Bitmap buttonMusicBitmap;
	private Bitmap buttonOnMusicBitmap;
	private MySprite btMusic;
	private Bitmap buttonSoundBitmap;
	private Bitmap buttonOnSoundBitmap;
	private MySprite btSound;
	private Bitmap logoCompleteBitmap;
	private MySprite logoComplete;
	private Bitmap logoOverBitmap;
	private MySprite logoOver;
	private Bitmap buttonNextBitmap;
	private MySprite btNext;
	private Bitmap buttonRestartBitmap;
	private MySprite btRestart;
	private Bitmap star1Bitmap;	
	private MySprite star1;
	private Bitmap star2Bitmap;
	private MySprite star2;
	private Bitmap star3Bitmap;
	private MySprite star3;
	private Bitmap starLightBitmap;
	private int aX;
	private int aY;
	private int aZ;
	private int numLevelCompleted = 0;
	private int currentLevel = 1;
	private LevelSelectSprite[] levelSelectSprite = new LevelSelectSprite[20];
	private Bitmap levelLockBitmap;
	private Bitmap levelOpenBitmap;
	private SharedPreferences sPreferences;
	private Editor editor;
	private boolean isNoDeath;
	private boolean isMusicOn;
	private boolean isSoundOn;
	private MySprite btMusicMain;
	private MySprite btSoundMain;
	private Bitmap helpTableBitmap;
	private Bitmap btBackBitmap;
	private MySprite btBack;
	private MySprite helpTable;
	private CountDownTimer timer;
	private CountDownTimer mTimer;
	private boolean isSetAlpha;
	private Bitmap bmpSuabien;
	private SuaBien suaBien;
	private List<SuaBien> suaBiens = new ArrayList<SuaBien>();
	private Bitmap fontScore;
	
	private Bitmap bmpSuaHeart;
	private SuaBien suaHeart;
	private boolean issuaheart = false;
	private int countheart = 2;
	

	public GameView(Context context) {
		super(context);
		gameThread = new GameThread(this);
		sPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
		editor = sPreferences.edit();
		setMusicOn(sPreferences.getBoolean(KEY_MUSIC, true));
		setSoundOn(sPreferences.getBoolean(KEY_SOUND, true));
		holder = getHolder();
		holder.addCallback(new SurfaceHolder.Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				boolean retry = true;
				gameThread.setRunning(false);
				while (retry) {
					try {
						gameThread.join();
						retry = false;
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {				
				loadGameResources();				
				initGame();			
				SoundManager.getInstance();
				SoundManager.initSounds(getContext());
				SoundManager.loadSounds();
				
				gameThread.setRunning(true);
				gameThread.setState(GameThread.STATE_SHOW_MENU);
				gameThread.start();	
			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
			}
		});
	}	
	
	protected void initGame() {
		mTimer = new CountDownTimer(0, 0) {			
			@Override
			public void onTick(long millisUntilFinished) {				
			}
			
			@Override
			public void onFinish() {
				setNoDeath(true);
				setSetAlpha(true);				
			}
		};
		timer = new CountDownTimer(5000, 5000) {			
			@Override
			public void onTick(long millisUntilFinished) {				
			}
			
			@Override
			public void onFinish() {
				setNoDeath(false);
				setSetAlpha(false);		
				//Het bao ve
				
			}
		};
		createButtonPause();
		createProgressBar();
		createButtonHelp();
		createButtonPlay();
		createButtonExit();
		createBackgroundMenu();
		createLogoPause();
		createLogoComplete();
		createLogoGameOver();
		createButtonMenu();
		createButtonResume();
		createButtonMusic();
		createButtonSound();
		createButtonNext();
		createButtonRestart();
		createButtonStar2();
		createButtonStar1();
		createButtonStar3();				
		createSprites();
		createFishes();					
		loadResourceTempSprites();
		loadResourceTempLevelUps();
		loadResourcesScoreText();
		loadResourcesLifeText();
		loadResourcesLevelNumText();
		createLevelLock();
		createButtonMusicMain();
		createButtonSoundMain();
		createHelpTable();
		createButtonBack();
	}	

	private void createHelpTable() {
		int x = getWidth() / 2 - helpTableBitmap.getWidth() / 2;
		int y = getHeight() / 2 - helpTableBitmap.getHeight() / 2;
		helpTable = new MySprite(this, helpTableBitmap, x, y);
	}

	private void createButtonBack() {
		int x = getWidth() / 2 - btBackBitmap.getWidth() / 2;
		int y = helpTable.getY() + helpTableBitmap.getHeight() - btBackBitmap.getHeight() - btBackBitmap.getHeight() / 4;
		btBack = new MySprite(this, btBackBitmap, x, y);
	}

	private void createButtonSoundMain() {
		int x = 0;
		int y = 0;
		btMusicMain = new MySprite(this, buttonMusicBitmap, x, y);
	}

	private void createButtonMusicMain() {
		int x = 0;
		int y = buttonMusicBitmap.getHeight();
		btSoundMain = new MySprite(this, buttonSoundBitmap, x, y);
	}

	private float scaleFishX = 0;
	private float scaleFishY = 0;
	protected void loadGameResources() {	
		
		Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        scaleY = (float) background.getHeight() / (float) getHeight();
        scaleX = (float) background.getWidth() / (float) getWidth();
		//scaleX=1;
		//scaleY=1;
        int newWidth = Math.round(background.getWidth() / scaleX);
        int newHeight = Math.round(background.getHeight() / scaleY);
        scaledBackGround = Bitmap.createScaledBitmap(background, newWidth, newHeight, true);
        background.recycle();
        
        Bitmap backgroundMenu = BitmapFactory.decodeResource(getResources(), R.drawable.bg_main_menu);
        int newWidthMenu = Math.round(backgroundMenu.getWidth() / scaleX);
        int newHeightMenu = Math.round(backgroundMenu.getHeight() / scaleY);
        mainMenuBackGround = Bitmap.createScaledBitmap(backgroundMenu, newWidthMenu, newHeightMenu, true);
        backgroundMenu.recycle();
        
        Bitmap suaBienTempBMP = BitmapFactory.decodeResource(getResources(), R.drawable.sua);
        int suaWidth = Math.round(suaBienTempBMP.getWidth() / scaleX);
        int suaheight = Math.round(suaBienTempBMP.getHeight() / scaleY);
        bmpSuabien = Bitmap.createScaledBitmap(suaBienTempBMP, suaWidth, suaheight, true);
        suaBienTempBMP.recycle();
        
        suaBienTempBMP = BitmapFactory.decodeResource(getResources(), R.drawable.heart);
        suaWidth = Math.round(suaBienTempBMP.getWidth() / scaleX);
        suaheight = Math.round(suaBienTempBMP.getHeight() / scaleY);
        bmpSuaHeart = Bitmap.createScaledBitmap(suaBienTempBMP, suaWidth, suaheight, true);
        suaBienTempBMP.recycle();
        
        
        Bitmap helpTableTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.help_table);
        int helpWidth = Math.round(helpTableTempBitmap.getWidth() / scaleX);
        int helpHeight = Math.round(helpTableTempBitmap.getHeight() / scaleY);
        helpTableBitmap = Bitmap.createScaledBitmap(helpTableTempBitmap, helpWidth, helpHeight, true);
        helpTableTempBitmap.recycle();
        
        Bitmap btBackTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_back_1);
        int backWidth = Math.round(btBackTempBitmap.getWidth() / scaleX);
        int backHeight = Math.round(btBackTempBitmap.getHeight() / scaleY);
        btBackBitmap = Bitmap.createScaledBitmap(btBackTempBitmap, backWidth, backHeight, true);
        btBackTempBitmap.recycle();
        
        Bitmap levelLockTemp = BitmapFactory.decodeResource(getResources(), R.drawable.level_lock);
        int width1 = Math.round(levelLockTemp.getWidth() / scaleX);
        int height1 = Math.round(levelLockTemp.getHeight() / scaleY);
        levelLockBitmap = Bitmap.createScaledBitmap(levelLockTemp, width1, height1, true);    
        levelLockTemp.recycle();
        
        Bitmap levelOpenTemp = BitmapFactory.decodeResource(getResources(), R.drawable.level_open);
        setLevelOpenBitmap(Bitmap.createScaledBitmap(levelOpenTemp, width1, height1, true)); 
        levelOpenTemp.recycle();
        
        Bitmap mScoreBar = BitmapFactory.decodeResource(getResources(), R.drawable.bar);
        int newWidth1 = Math.round(mScoreBar.getWidth() / scaleX);
        int newHeight1 = Math.round(mScoreBar.getHeight() / scaleY);
        scoreBarScaled = Bitmap.createScaledBitmap(mScoreBar, newWidth1, newHeight1, true);
        mScoreBar.recycle();
        
        Bitmap sBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pause);
        int width111 = Math.round(sBitmap.getWidth() / scaleX);
        int height111 = Math.round(sBitmap.getHeight() / scaleX);
        buttonPauseBitmap = Bitmap.createScaledBitmap(sBitmap, width111, height111, true); 
        Bitmap s1Bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.unpause);
        buttonUnPauseBitmap = Bitmap.createScaledBitmap(s1Bitmap, width111, height111, true);
        sBitmap.recycle();
        s1Bitmap.recycle();
        
		
		Bitmap pro = BitmapFactory.decodeResource(getResources(), R.drawable.progress);
		int proNewWidth = Math.round(pro.getWidth() / scaleX);
		int proNewHeight = Math.round(pro.getHeight() / scaleY);
		progressBitmap = Bitmap.createScaledBitmap(pro, proNewWidth, proNewHeight, true);				
		progressToWin = progressBitmap.getWidth();
		pro.recycle();
		
		Bitmap sep1 = BitmapFactory.decodeResource(getResources(), R.drawable.separate);
		int newSep1Width = Math.round(sep1.getWidth() / scaleX);
		int newSep1Height = Math.round(sep1.getHeight() / scaleY);
		separate1 = Bitmap.createScaledBitmap(sep1, newSep1Width, newSep1Height, true);	
		life = MAX_LIFE;
		score = 0;
		sep1.recycle();
		
		
		scaleFishX = scaleX-0.15f;
		scaleFishY = scaleY-0.15f;
		if(scaleFishX>1){
			scaleFishX=1.0f;
		}
		if(scaleFishY>1){
			scaleFishY=1.0f;
		}
		
		
		
		Bitmap fishSmallTemp = BitmapFactory.decodeResource(getResources(), R.drawable.f0);
		int newWidthSmall1 = Math.round(fishSmallTemp.getWidth() / scaleFishX);
		int newHeightSmall1 = Math.round(fishSmallTemp.getHeight() / scaleFishY);
		fishSmallBitmap = Bitmap.createScaledBitmap(fishSmallTemp, newWidthSmall1, newHeightSmall1, true);
		fishSmallTemp.recycle();
		
		Bitmap fishMediumTemp = BitmapFactory.decodeResource(getResources(), R.drawable.f1);
		int newWidthMedium1 = Math.round(fishMediumTemp.getWidth() / scaleFishX);
		int newHeightMedium1 = Math.round(fishMediumTemp.getHeight() / scaleFishY);
        fishMediumBitmap = Bitmap.createScaledBitmap(fishMediumTemp, newWidthMedium1, newHeightMedium1, true);
        fishMediumTemp.recycle();
        
        Bitmap fishBigTemp = BitmapFactory.decodeResource(getResources(), R.drawable.f2);
        int newWidthBig = Math.round(fishBigTemp.getWidth() / scaleFishX);
        int newHeightBig = Math.round(fishBigTemp.getHeight() / scaleFishY);
        fishBigBitmap = Bitmap.createScaledBitmap(fishBigTemp, newWidthBig, newHeightBig, true);
        fishBigTemp.recycle();
		
		Bitmap fishSmallTemp2 = BitmapFactory.decodeResource(getResources(), R.drawable.f5);
		int newWidthSmall2 = Math.round(fishSmallTemp2.getWidth() / scaleFishX);
        int newHeightSmall2 = Math.round(fishSmallTemp2.getHeight() / scaleFishY);
		spriteBigBitmap1 = Bitmap.createScaledBitmap(fishSmallTemp2, newWidthSmall2, newHeightSmall2, true);
		fishSmallTemp2.recycle();
		
		Bitmap fishSmallTemp3 = BitmapFactory.decodeResource(getResources(), R.drawable.f8);
		int newWidthSmall3 = Math.round(fishSmallTemp3.getWidth() / scaleFishX);
        int newHeightSmall3 = Math.round(fishSmallTemp3.getHeight() / scaleFishY);
		spriteBigBitmap2 = Bitmap.createScaledBitmap(fishSmallTemp3, newWidthSmall3, newHeightSmall3, true);
		fishSmallTemp3.recycle();
		
		Bitmap fishSmallTemp4 = BitmapFactory.decodeResource(getResources(), R.drawable.f10);
		int newWidthSmall4 = Math.round(fishSmallTemp4.getWidth() / scaleFishX);
        int newHeightSmall4 = Math.round(fishSmallTemp4.getHeight() / scaleFishY);
		spriteBigBitmap3 = Bitmap.createScaledBitmap(fishSmallTemp4, newWidthSmall4, newHeightSmall4, true);	
		fishSmallTemp4.recycle();
        
        Bitmap fishMediumTemp2 = BitmapFactory.decodeResource(getResources(), R.drawable.f17);
		int newWidthMedium2= Math.round(fishMediumTemp2.getWidth() / scaleFishX);
        int newHeightMedium2 = Math.round(fishMediumTemp2.getHeight() / scaleFishY);
        spriteBigBitmap4 = Bitmap.createScaledBitmap(fishMediumTemp2, newWidthMedium2, newHeightMedium2, true);
        fishMediumTemp2.recycle();
        
		Bitmap fishMediumTemp3 = BitmapFactory.decodeResource(getResources(), R.drawable.f14);
		int newWidthMedium3 = Math.round(fishMediumTemp3.getWidth() / scaleFishX);
        int newHeightMedium3 = Math.round(fishMediumTemp3.getHeight() / scaleFishY);
        spriteMediumBitmap1 = Bitmap.createScaledBitmap(fishMediumTemp3, newWidthMedium3, newHeightMedium3, true);
        fishMediumTemp3.recycle();
		
		Bitmap fishTempBig2 = BitmapFactory.decodeResource(getResources(), R.drawable.f14);
		int newWidthBig2 = Math.round(fishTempBig2.getWidth() / scaleFishX);
        int newHeightBig2 = Math.round(fishTempBig2.getHeight() / scaleFishY);
		spriteMediumBitmap2 = Bitmap.createScaledBitmap(fishTempBig2, newWidthBig2, newHeightBig2, true);
		fishTempBig2.recycle();
		
		Bitmap fishTempBig3 = BitmapFactory.decodeResource(getResources(), R.drawable.f14);
		int newWidthBig3 = Math.round(fishTempBig3.getWidth() / scaleFishX);
        int newHeightBig3 = Math.round(fishTempBig3.getHeight() / scaleFishY);
		spriteMediumBitmap3 = Bitmap.createScaledBitmap(fishTempBig3, newWidthBig3, newHeightBig3, true);
		fishTempBig3.recycle();
		
		Bitmap fishTempBig4 = BitmapFactory.decodeResource(getResources(), R.drawable.f14);
		int newWidthBig4 = Math.round(fishTempBig4.getWidth() / scaleFishX);
        int newHeightBig4 = Math.round(fishTempBig4.getHeight() / scaleFishY);
		spriteMediumBitmap4 = Bitmap.createScaledBitmap(fishTempBig4, newWidthBig4, newHeightBig4, true);
		fishTempBig4.recycle();
		
		Bitmap spriteTempSmall1 = BitmapFactory.decodeResource(getResources(), R.drawable.f12);
		int newWidthSpriteSmall1 = Math.round(spriteTempSmall1.getWidth() / scaleFishX);
        int newHeightSpriteSmall1 = Math.round(spriteTempSmall1.getHeight() / scaleFishY);
		spriteSmallBitmap1 = Bitmap.createScaledBitmap(spriteTempSmall1, newWidthSpriteSmall1, newHeightSpriteSmall1, true);
		spriteTempSmall1.recycle();
		
		Bitmap spriteTempSmall2 = BitmapFactory.decodeResource(getResources(), R.drawable.f13);
		int newWidthSpriteSmall2 = Math.round(spriteTempSmall2.getWidth() / scaleFishX);
        int newHeightSpriteSmall2 = Math.round(spriteTempSmall2.getHeight() / scaleFishY);
		spriteSmallBitmap2 = Bitmap.createScaledBitmap(spriteTempSmall2, newWidthSpriteSmall2, newHeightSpriteSmall2, true);
		spriteTempSmall2.recycle();
		
		Bitmap spriteTempSmall3 = BitmapFactory.decodeResource(getResources(), R.drawable.f19);
		int newWidthSpriteSmall3 = Math.round(spriteTempSmall3.getWidth() / scaleFishX);
        int newHeightSpriteSmall3 = Math.round(spriteTempSmall3.getHeight() / scaleFishY);
		spriteSmallBitmap3 = Bitmap.createScaledBitmap(spriteTempSmall3, newWidthSpriteSmall3, newHeightSpriteSmall3, true);
		spriteTempSmall3.recycle();
		
		
		Bitmap spriteTempSmall4 = BitmapFactory.decodeResource(getResources(), R.drawable.f12);
		int newWidthSpriteSmall4 = Math.round(spriteTempSmall4.getWidth() / scaleFishX);
        int newHeightSpriteSmall4 = Math.round(spriteTempSmall4.getHeight() / scaleFishY);
		spriteSmallBitmap4 = Bitmap.createScaledBitmap(spriteTempSmall4, newWidthSpriteSmall4, newHeightSpriteSmall4, true);
		spriteTempSmall4.recycle();	
		
		Bitmap helpTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_help_1);
		int x1 = Math.round(helpTempBitmap.getWidth() / scaleX);
        int y1 = Math.round(helpTempBitmap.getHeight() / scaleY);
		buttonHelpBitmap = Bitmap.createScaledBitmap(helpTempBitmap, x1, y1, true);
		helpTempBitmap.recycle();
		
		
		Bitmap playTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_play_1);
		buttonPlayBitmap = Bitmap.createScaledBitmap(playTempBitmap, x1, y1, true);
		playTempBitmap.recycle();
		
		
		Bitmap exitTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_exit_1);
		buttonExitBitmap = Bitmap.createScaledBitmap(exitTempBitmap, x1, y1, true);
		exitTempBitmap.recycle();
						
		
		Bitmap bgMenuTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_menu);
		int x2 = Math.round(bgMenuTempBitmap.getWidth() / scaleX);
        int y2 = Math.round(bgMenuTempBitmap.getHeight() / scaleY);
		bg_menu_Bitmap = Bitmap.createScaledBitmap(bgMenuTempBitmap, x2, y2, true);
		bgMenuTempBitmap.recycle();
		
		
		Bitmap logoPauseTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pause_text);
		int x3 = Math.round(logoPauseTempBitmap.getWidth() / scaleX);
        int y3 = Math.round(logoPauseTempBitmap.getHeight() / scaleY);
		logoPauseBitmap = Bitmap.createScaledBitmap(logoPauseTempBitmap, x3, y3, true);
		logoPauseTempBitmap.recycle();
		
		
		Bitmap logoCompleTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.win);
		int x5 = Math.round(logoCompleTempBitmap.getWidth() / scaleX);
        int y5 = Math.round(logoCompleTempBitmap.getHeight() / scaleY);
		logoCompleteBitmap = Bitmap.createScaledBitmap(logoCompleTempBitmap, x5, y5, true);
		logoCompleTempBitmap.recycle();
		
		
		Bitmap logoGameOverTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.over);
		int x6 = Math.round(logoGameOverTempBitmap.getWidth() / scaleX);
        int y6 = Math.round(logoGameOverTempBitmap.getHeight() / scaleY);
		logoOverBitmap = Bitmap.createScaledBitmap(logoGameOverTempBitmap, x6, y6, true);
		logoGameOverTempBitmap.recycle();
		
		
		Bitmap menuTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_menu1);
		buttonMenuBitmap = Bitmap.createScaledBitmap(menuTempBitmap, x1, y1, true);
		menuTempBitmap.recycle();
		
		
		Bitmap resumeTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_resume_1);
		buttonResumeBitmap = Bitmap.createScaledBitmap(resumeTempBitmap, x1, y1, true);
		resumeTempBitmap.recycle();
		
		
		Bitmap musicTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_off_music);
		Bitmap musicOnTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_on_music);
		int x4 = Math.round(musicTempBitmap.getWidth() / scaleX);
        int y4 = Math.round(musicTempBitmap.getHeight() / scaleY);
		buttonMusicBitmap = Bitmap.createScaledBitmap(musicTempBitmap, x4, y4, true);
		buttonOnMusicBitmap = Bitmap.createScaledBitmap(musicOnTempBitmap, x4, y4, true);
		musicTempBitmap.recycle();
		musicOnTempBitmap.recycle();
		
		
		Bitmap soundTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_off_sound);
		Bitmap soundOnTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bt_on_sound);
		buttonSoundBitmap = Bitmap.createScaledBitmap(soundTempBitmap, x4, y4, true);
		buttonOnSoundBitmap = Bitmap.createScaledBitmap(soundOnTempBitmap, x4, y4, true);
		soundTempBitmap.recycle();
		soundOnTempBitmap.recycle();
		
		
		Bitmap nextTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.next_1);
		buttonNextBitmap = Bitmap.createScaledBitmap(nextTempBitmap, x1, y1, true);
		nextTempBitmap.recycle();
		
		
		Bitmap restartTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.restart_1);
		buttonRestartBitmap = Bitmap.createScaledBitmap(restartTempBitmap, x1, y1, true);
		restartTempBitmap.recycle();
		
		
		Bitmap star2TempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star_dark);
		int x7 = Math.round(star2TempBitmap.getWidth() / scaleX);
        int y7 = Math.round(star2TempBitmap.getHeight() / scaleY);
		star2Bitmap = Bitmap.createScaledBitmap(star2TempBitmap, x7, y7, true);
		star2TempBitmap.recycle();
		
		
		Bitmap star1TempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star_dark);
		star1Bitmap = Bitmap.createScaledBitmap(star1TempBitmap, x7, y7, true);
		
		
		Bitmap star3TempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star_dark);
		star3Bitmap = Bitmap.createScaledBitmap(star3TempBitmap, x7, y7, true);
		star3TempBitmap.recycle();
		
		
		Bitmap starLightTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star_light);
		starLightBitmap = Bitmap.createScaledBitmap(starLightTempBitmap, x7, y7, true);
		starLightTempBitmap.recycle();
	}

	private void createLevelLock() {
		int x = levelLockBitmap.getWidth() / 4;
		int y = levelLockBitmap.getHeight() / 4;
		int w = levelLockBitmap.getWidth();
		int h = levelLockBitmap.getHeight();
		
		for (int i = 0; i < 5; i++) {
			levelSelectSprite[i] = new LevelSelectSprite(this, levelLockBitmap, null, x + w + (i*w), y, false, 1);
		}
		
		for (int i = 5; i < 10; i++) {
			levelSelectSprite[i] = new LevelSelectSprite(this, levelLockBitmap, null, x + w + ((i%5)*w), y + h, false, 1);
		}
		
		for (int i = 10; i < 15; i++) {
			levelSelectSprite[i] = new LevelSelectSprite(this, levelLockBitmap, null, x + w + ((i%5)*w), y + (2*h), false, 1);
		}
		
		for (int i = 15; i < 20; i++) {
			levelSelectSprite[i] = new LevelSelectSprite(this, levelLockBitmap, null, x + w + ((i%5)*w), y + (3*h), false, 1);
		}
	}

	protected void createButtonStar2() {
		int x = getWidth() / 2 - star2Bitmap.getWidth() / 2;
		int y = (this.getHeight() / 2) - star2Bitmap.getHeight() / 2 - star2Bitmap.getHeight() / 4;
		star2 = new MySprite(this, star2Bitmap, x, y);
	}

	protected void createButtonStar1() {
		int x = star2.getX() - star1Bitmap.getWidth();
		int y = (this.getHeight() / 2) - star1Bitmap.getHeight() / 2 - star1Bitmap.getHeight() / 4;
		star1 = new MySprite(this, star1Bitmap, x, y);
	}

	protected void createButtonStar3() {
		int x = getWidth() / 2 + star3Bitmap.getWidth() / 2;
		int y = (this.getHeight() / 2) - star3Bitmap.getHeight() / 2 - star3Bitmap.getHeight() / 4;
		star3 = new MySprite(this, star3Bitmap, x, y);
	}

	protected void createLogoGameOver() {
		int x = getWidth() / 2 - logoOverBitmap.getWidth() / 2;
		int y = bgMenu.getX() - logoOverBitmap.getHeight();
		logoOver = new MySprite(this, logoOverBitmap, x, y);
	}

	protected void createLogoComplete() {
		int x = getWidth() / 2 - logoCompleteBitmap.getWidth() / 2;
		int y = bgMenu.getX() - logoCompleteBitmap.getHeight();
		logoComplete = new MySprite(this, logoCompleteBitmap, x, y);
	}

	protected void createButtonRestart() {
		int x = bgMenu.getX() + bgMenu.getWidth() - buttonRestartBitmap.getWidth() - buttonRestartBitmap.getWidth() / 6;
		int y = (this.getHeight() / 2) + buttonRestartBitmap.getHeight();
		btRestart = new MySprite(this, buttonRestartBitmap, x, y);
	}

	protected void createButtonNext() {
		int x = bgMenu.getX() + bgMenu.getWidth() - buttonNextBitmap.getWidth() - buttonNextBitmap.getWidth() / 6;
		int y = (this.getHeight() / 2) + buttonNextBitmap.getHeight();
		btNext = new MySprite(this, buttonNextBitmap, x, y);
	}

	protected void createButtonMusic() {
		int x = getWidth() / 2 - buttonMusicBitmap.getWidth() - buttonMusicBitmap.getWidth() / 2;
		int y = (this.getHeight() / 2) - buttonMusicBitmap.getHeight() / 2;
		btMusic = new MySprite(this, buttonMusicBitmap, x, y);
	}

	protected void createButtonSound() {
		int x = getWidth() / 2 + buttonSoundBitmap.getWidth() / 2;
		int y = (this.getHeight() / 2) - buttonSoundBitmap.getHeight() / 2;
		btSound = new MySprite(this, buttonSoundBitmap, x, y);
	}

	protected void createButtonResume() {
		int x = bgMenu.getX() + bgMenu.getWidth() - buttonResumeBitmap.getWidth() - buttonResumeBitmap.getWidth() / 6;
		int y = (this.getHeight() / 2) + buttonResumeBitmap.getHeight();
		btResume = new MySprite(this, buttonResumeBitmap, x, y);
	}

	protected void createButtonMenu() {
		int x = bgMenu.getX() + buttonMenuBitmap.getWidth() / 6;
		int y = (this.getHeight() / 2) + buttonMenuBitmap.getHeight();
		btMenu = new MySprite(this, buttonMenuBitmap, x, y);
	}

	protected void createLogoPause() {
		int x = getWidth() / 2 - logoPauseBitmap.getWidth() / 2;
		int y = bgMenu.getX() - logoPauseBitmap.getHeight() / 2 - logoPauseBitmap.getHeight() / 5;
		logoPause = new MySprite(this, logoPauseBitmap, x, y);
	}

	protected void createBackgroundMenu() {
		int x = getWidth() / 2 - bg_menu_Bitmap.getWidth() / 2;
		int y = getHeight() / 2 - bg_menu_Bitmap.getHeight() / 2;
		bgMenu = new MySprite(this, bg_menu_Bitmap, x, y);
	}

	protected void createButtonPause() {
		int x = this.getWidth() - buttonPauseBitmap.getWidth() + buttonPauseBitmap.getWidth() / 14;
		int y = - buttonPauseBitmap.getHeight() / 10;
		btPause = new MySprite(this, buttonPauseBitmap, x, y);
	}

	protected void createButtonHelp() {
		int x = (this.getWidth() / 2) - (buttonHelpBitmap.getWidth() / 2);
		int y = (this.getHeight() / 2) - (buttonHelpBitmap.getHeight() / 2) + buttonHelpBitmap.getHeight();
		btHelp = new MySprite(this, buttonHelpBitmap, x, y);
	}
	
	protected void createButtonPlay() {
		int x = (this.getWidth() / 2) - (buttonPlayBitmap.getWidth() / 2);
		int y = (this.getHeight() / 2) - (buttonPlayBitmap.getHeight() / 2);
		btPlay = new MySprite(this, buttonPlayBitmap, x, y);
	}

	protected void createButtonExit() {
		int x = (this.getWidth() / 2) - (buttonExitBitmap.getWidth() / 2);
		int y = (this.getHeight() / 2) - (buttonExitBitmap.getHeight() / 2) + buttonHelpBitmap.getHeight() * 2;
		btExit = new MySprite(this, buttonExitBitmap, x, y);
	}

	protected void loadResourcesScoreText() {		
		Bitmap font = BitmapFactory.decodeResource(getResources(), R.drawable.bmp_score);
		int newWidth = Math.round(font.getWidth() / scaleX);
        int newHeight = Math.round(font.getHeight() / scaleY);
        this.mBitmapFont = new BitmapFont(Bitmap.createScaledBitmap(font, newWidth, newHeight, true));
	}
	
	protected void loadResourcesLifeText() {		
		Bitmap font = BitmapFactory.decodeResource(getResources(), R.drawable.bmp_life);
		int newWidth = Math.round(font.getWidth() / scaleX);
        int newHeight = Math.round(font.getHeight() / scaleY);
        this.mBitmapFontLife = new BitmapFont(Bitmap.createScaledBitmap(font, newWidth, newHeight, true));
	}
	
	protected void loadResourcesLevelNumText() {		
		Bitmap font = BitmapFactory.decodeResource(getResources(), R.drawable.level_num);
		int newWidth = Math.round(font.getWidth() / scaleX);
        int newHeight = Math.round(font.getHeight() / scaleY);
        fontScore = Bitmap.createScaledBitmap(font, newWidth, newHeight, true);
        this.mBitmapFontNumLevel = new BitmapFont(fontScore);
	}
	
	protected void createSprites() {
		
        for(int i = 0; i < SMALL_FISH_NUM; i++){
        	spritesSmall.add(createSprite(R.drawable.f12));
        }
        for(int i = 0; i < MEDIUM_FISH_NUM; i++){
        	spritesMedium.add(createSprite(R.drawable.f14));
        }    
        for(int i = 0; i < BIG_FISH_NUM; i++){
        	spritesBig.add(createSprite(R.drawable.f5));
        } 
	}
	
	public void removeSprites() {
		
        for(int i = 0; i < SMALL_FISH_NUM; i++){
        	spritesSmall.remove(i);
        }
        for(int i = 0; i < MEDIUM_FISH_NUM; i++){
        	spritesMedium.remove(i);
        }    
        for(int i = 0; i < BIG_FISH_NUM; i++){
        	spritesBig.remove(i);
        } 
	}
	
	protected Sprite createSprite(int resource) {
		Bitmap fish = BitmapFactory.decodeResource(getResources(), resource);
		int newWidth = Math.round(fish.getWidth() / scaleFishX);
        int newHeight = Math.round(fish.getHeight() / scaleFishY);
        Bitmap bmp = Bitmap.createScaledBitmap(fish, newWidth, newHeight, true);
        return new Sprite(this,bmp);
	}
	
	protected void createSuaBiens() {
		for(int i = 0; i < currentLevel; i++) {
			suaBiens.add(createSuaBien());		
		}						
	}
	
	protected void removeSuaBiens() {
		if (suaBiens.size() >= 1) {
			suaBiens.clear();
		}				
	}
	
	protected SuaBien createSuaBien() {
		return new SuaBien(this, bmpSuabien);
	}
	
	protected SuaBien createSuaHeart() {
		return new SuaBien(this, bmpSuaHeart);
	}
	
	
	
	protected void createFishes() {
        fishes.add(createFish(R.drawable.f0));
	}
	
	protected Fish createFish(int resource) {
        return new Fish(this,fishSmallBitmap);
	}
	
	protected void loadResourceTempSprites(){
		Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.ef0);
		int newWidth = Math.round(temp.getWidth() / scaleX);
        int newHeight = Math.round(temp.getHeight() / scaleY);
		bmpTemp = Bitmap.createScaledBitmap(temp, newWidth, newHeight, true);		
	}
	
	protected void loadResourceTempLevelUps(){
		Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.levelup);
		int newWidth = Math.round(temp.getWidth() / scaleX);
        int newHeight = Math.round(temp.getHeight() / scaleY);
		bmpTempLevelUp = Bitmap.createScaledBitmap(temp, newWidth, newHeight, true);		
	}
	
	protected MyProgressBar createProgressBar(){
		mProgressBar = new MyProgressBar(this, progressBitmap, 
				this.getWidth() / 2 - progressBitmap.getWidth() / 2, 
				scoreBarScaled.getHeight() / 2 - progressBitmap.getHeight());
		return mProgressBar;
	}
	
	/**
	 * Ve cac doi tuong len man hinh chinh cua game
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);		
		canvas.drawBitmap(scaledBackGround, 0, 0, null);
		
		if(issuaheart){
			suaHeart.draw(canvas);
//			if(Math.random()<0.02){
//				issuaheart=false;
//				suaHeart=null;
//			}
		}else{
			if(countheart>0){
				if(Math.random()<0.2){
					countheart--;
					suaHeart = createSuaHeart();
					issuaheart=true;
				}
			}
		}
		
		for (int i = 0; i < currentLevel; i++) {
            suaBiens.get(i).draw(canvas);
		}
			
		
		for (Sprite sprite : spritesMedium) {            
            sprite.setAddSpeed(currentLevel);
            sprite.draw(canvas);
		}
		for (Sprite sprite : spritesSmall) {            
            sprite.setAddSpeed(currentLevel);
            sprite.draw(canvas);
		}
		
		for (Sprite sprite : spritesBig) {
			sprite.setAddSpeed(currentLevel);
            sprite.draw(canvas);            
		}
		
		for (Fish fish : fishes) {
            fish.draw(canvas);
		}				
		
		for (int i = tempSprites.size() - 1; i >= 0; i--) {
            tempSprites.get(i).draw(canvas);
		}	
		
		for (int i = tempLevelUps.size() - 1; i >= 0; i--) {
            tempLevelUps.get(i).draw(canvas);
		}
		
		canvas.drawBitmap(scoreBarScaled, 0, 0, null);
		mProgressBar.draw(canvas);
		canvas.drawBitmap(separate1, getWidth() / 2 - separate1.getWidth() / 2 - progressBitmap.getWidth() / 6, (int)((scoreBarScaled.getHeight() / 4.7)), null);
		canvas.drawBitmap(separate1, getWidth() / 2 + progressBitmap.getWidth() / 6 + separate1.getWidth() / 2, (int)((scoreBarScaled.getHeight() / 4.7)), null);
		btPause.draw(canvas);
		scoreString = String.valueOf(score);
		mBitmapFont.drawString(canvas, scoreString, scoreBarScaled.getWidth() / 15, (int)(scoreBarScaled.getHeight() * 0.23));
		lifeString = String.valueOf(life);
		mBitmapFontLife.drawString(canvas, lifeString, (int)(scoreBarScaled.getWidth() * 0.835), (int)(scoreBarScaled.getHeight() * 0.22));		
		levelString = String.valueOf(currentLevel);
		mBitmapFont.drawString(canvas, levelString, (int)(scoreBarScaled.getWidth() / 4.35), (int)(scoreBarScaled.getHeight() * 0.22));
		lose();
		win();		
	}
	
	/**
	 * Ve menu chinh cua game
	 * @param canvas
	 */
	public void drawMainMenu(Canvas canvas) {
		canvas.drawColor(Color.BLACK);		
		canvas.drawBitmap(mainMenuBackGround, 0, 0, null);
		btHelp.draw(canvas);
		btPlay.draw(canvas);
		btExit.draw(canvas);	
		btMusicMain.draw(canvas);
		btSoundMain.draw(canvas);
	}
	
	/**
	 * Ve menu chon level choi
	 * @param canvas
	 */
	public void drawLevelMenu(Canvas canvas) {
		canvas.drawColor(Color.BLACK);		
		canvas.drawBitmap(scaledBackGround, 0, 0, null);
		for (int i = 0; i < 20; i++) {
			levelSelectSprite[i].draw(canvas);
		}
		
		for (int j = 0; j <= sPreferences.getInt(KEY_NUMBER_LEVEL, 0); j++) {
			if (j < 9) {
				mBitmapFontNumLevel.drawString(canvas, String.valueOf(j + 1), levelSelectSprite[j].getX() + (levelSelectSprite[j].getWidth() / 2) - fontScore.getWidth() / 20,
						levelSelectSprite[j].getY() + levelSelectSprite[j].getHeight() / 2 - fontScore.getHeight() / 2);
			} else if (j >= 9) {
				mBitmapFontNumLevel.drawString(canvas, String.valueOf(j + 1), levelSelectSprite[j].getX() + (levelSelectSprite[j].getWidth() / 2) - fontScore.getWidth() / 10,
						levelSelectSprite[j].getY() + levelSelectSprite[j].getHeight() / 2 - fontScore.getHeight());
			}			
		}
	}
	
	/**
	 * Ve menu pause game
	 * @param canvas
	 */
	public void drawMenuPause(Canvas canvas) {
		canvas.drawColor(Color.BLACK);		
		canvas.drawBitmap(scaledBackGround, 0, 0, null);
		bgMenu.draw(canvas);
		canvas.drawBitmap(scoreBarScaled, 0, 0, null);
		mProgressBar.draw(canvas);
		canvas.drawBitmap(separate1, getWidth() / 2 - separate1.getWidth() / 2 - progressBitmap.getWidth() / 6, (int)((scoreBarScaled.getHeight() / 4.7)), null);
		canvas.drawBitmap(separate1, getWidth() / 2 + progressBitmap.getWidth() / 6 + separate1.getWidth() / 2, (int)((scoreBarScaled.getHeight() / 4.7)), null);
		btPause.draw(canvas);
		scoreString = String.valueOf(score);
		mBitmapFont.drawString(canvas, scoreString, scoreBarScaled.getWidth() / 15, (int)(scoreBarScaled.getHeight() * 0.23));
		lifeString = String.valueOf(life);
		mBitmapFontLife.drawString(canvas, lifeString, (int)(scoreBarScaled.getWidth() * 0.835), (int)(scoreBarScaled.getHeight() * 0.22));		
		levelString = String.valueOf(currentLevel);
		mBitmapFont.drawString(canvas, levelString, (int)(scoreBarScaled.getWidth() / 4.35), (int)(scoreBarScaled.getHeight() * 0.22));
		logoPause.draw(canvas);
		btMenu.draw(canvas);
		btResume.draw(canvas);
		btMusic.draw(canvas);
		btSound.draw(canvas);
	}
	
	/**
	 * Ve menu complete level
	 * @param canvas
	 */
	public void drawMenuComplete(Canvas canvas) {
		canvas.drawColor(Color.BLACK);		
		canvas.drawBitmap(scaledBackGround, 0, 0, null);
		bgMenu.draw(canvas);
		canvas.drawBitmap(scoreBarScaled, 0, 0, null);
		mProgressBar.draw(canvas);
		canvas.drawBitmap(separate1, getWidth() / 2 - separate1.getWidth() / 2 - progressBitmap.getWidth() / 6, (int)((scoreBarScaled.getHeight() / 4.7)), null);
		canvas.drawBitmap(separate1, getWidth() / 2 + progressBitmap.getWidth() / 6 + separate1.getWidth() / 2, (int)((scoreBarScaled.getHeight() / 4.7)), null);
		btPause.draw(canvas);
		scoreString = String.valueOf(score);
		mBitmapFont.drawString(canvas, scoreString, scoreBarScaled.getWidth() / 15, (int)(scoreBarScaled.getHeight() * 0.23));
		lifeString = String.valueOf(life);
		mBitmapFontLife.drawString(canvas, lifeString, (int)(scoreBarScaled.getWidth() * 0.835), (int)(scoreBarScaled.getHeight() * 0.22));		
		levelString = String.valueOf(currentLevel);
		mBitmapFont.drawString(canvas, levelString, (int)(scoreBarScaled.getWidth() / 4.35), (int)(scoreBarScaled.getHeight() * 0.22));
		btMenu.draw(canvas);
		btNext.draw(canvas);
		logoComplete.draw(canvas);
		star2.draw(canvas);
		star1.draw(canvas);
		star3.draw(canvas);
	}
	
	/**
	 * Ve menu game over
	 * @param canvas
	 */
	public void drawMenuOver(Canvas canvas) {
		canvas.drawColor(Color.BLACK);		
		canvas.drawBitmap(scaledBackGround, 0, 0, null);
		bgMenu.draw(canvas);
		canvas.drawBitmap(scoreBarScaled, 0, 0, null);
		mProgressBar.draw(canvas);
		canvas.drawBitmap(separate1, getWidth() / 2 - separate1.getWidth() / 2 - progressBitmap.getWidth() / 6, (int)((scoreBarScaled.getHeight() / 4.7)), null);
		canvas.drawBitmap(separate1, getWidth() / 2 + progressBitmap.getWidth() / 6 + separate1.getWidth() / 2, (int)((scoreBarScaled.getHeight() / 4.7)), null);
		btPause.draw(canvas);
		scoreString = String.valueOf(score);
		mBitmapFont.drawString(canvas, scoreString, scoreBarScaled.getWidth() / 15, (int)(scoreBarScaled.getHeight() * 0.23));
		lifeString = String.valueOf(life);
		mBitmapFontLife.drawString(canvas, lifeString, (int)(scoreBarScaled.getWidth() * 0.835), (int)(scoreBarScaled.getHeight() * 0.22));		
		levelString = String.valueOf(currentLevel);
		mBitmapFont.drawString(canvas, levelString, (int)(scoreBarScaled.getWidth() / 4.35), (int)(scoreBarScaled.getHeight() * 0.22));
		btMenu.draw(canvas);
		btRestart.draw(canvas);
		logoOver.draw(canvas);
		star2.draw(canvas);
		star1.draw(canvas);
		star3.draw(canvas);
	}
	
	/**
	 * Ve bang giup  do (help)
	 * @param canvas
	 */
	public void drawHelp(Canvas canvas) {
		canvas.drawColor(Color.BLACK);		
		canvas.drawBitmap(scaledBackGround, 0, 0, null);
		helpTable.draw(canvas);
		btBack.draw(canvas);
	}
	
	private void lose() {
		if (life < MIN_LIFE) {
			life = MIN_LIFE;
			gameThread.setState(GameThread.STATE_LOSE);
			star1.setBmp(star1Bitmap);
			star2.setBmp(star2Bitmap);
			star3.setBmp(star3Bitmap);
			if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
        		SoundManager.playSound(SOUND_OVER, SOUND_SPEED);
			}
		}	
	}
	
	private void win() {
		if (progress >= progressToWin && life >= MIN_LIFE) {			
			gameThread.setState(GameThread.STATE_WIN);	
			if (score >= 1000) {
				star1.setBmp(starLightBitmap);
				star2.setBmp(starLightBitmap);
				star3.setBmp(starLightBitmap);
			} else if (score >= 700 && score < 1000) {
				star1.setBmp(starLightBitmap);
				star2.setBmp(starLightBitmap);
				star3.setBmp(star3Bitmap);
			} else {
				star1.setBmp(starLightBitmap);
				star2.setBmp(star2Bitmap);
				star3.setBmp(star3Bitmap);
			}
			if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
        		SoundManager.playSound(SOUND_WIN, SOUND_SPEED);
			}
		}
	}
	
	public void update() {
		if (sPreferences.getBoolean(KEY_MUSIC, true) == false) {
			SoundManager.stopSound(MUSIC_BG);
		}
		synchronized (getHolder()) {			
			for (int i = spritesBig.size()-1; i >= 0; i--) {
				for (int j = spritesMedium.size()-1; j >= 0; j--) {
					for (int k = spritesSmall.size()-1; k >= 0; k--) {
						for (int n = fishes.size()-1; n >= 0; n--) {
							spriteBig = spritesBig.get(i);
				            spriteMedium = spritesMedium.get(j);
				            spriteSmall = spritesSmall.get(k);
				            fish = fishes.get(n);		
				            spriteSmall.setAddSpeed(currentLevel);
				            spriteMedium.setAddSpeed(currentLevel);
				            spriteBig.setAddSpeed(currentLevel);
				            
//				            if (spriteBig.collidesWith(spriteSmall) || spriteMedium.collidesWith(spriteSmall)) {
//				            	int x = spriteSmall.getX() - (bmpTemp.getWidth() / 6);
//				            	int y = spriteSmall.getY() - (bmpTemp.getHeight() / 6);
//				            	spritesSmall.remove(spriteSmall);
//				            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
//				            	spritesSmall.add(createSprite(R.drawable.f12));
//				            	break;
//				            } else
//				            if (spriteBig.collidesWith(spriteMedium)) {
//				            	int x = spriteMedium.getX() - (bmpTemp.getWidth() / 6);
//				            	int y = spriteMedium.getY() - (bmpTemp.getHeight() / 6);
//				            	spritesMedium.remove(spriteMedium);
//				            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
//				            	spritesMedium.add(createSprite(R.drawable.f14));
//				            	break;
//				            } else
				            
				            if (isSetAlpha == true) {
				            	fish.getPaint().setAlpha(200);
				            //	fish.setBmpAlpha(fishBitmapOpen);
							} else {
								fish.getPaint().setAlpha(255);
							//	fish.setIsAlpha(false);
							}	    
				            
				            if (currentLevel >= 1 && currentLevel < 5) {
				            	spriteSmall.setBitmap(spriteSmallBitmap1);
				            	spriteSmall.setWidth(spriteSmallBitmap1.getWidth() / 2);
				            	spriteSmall.setHeight(spriteSmallBitmap1.getHeight() / 2);
				            	
				            	spriteMedium.setBitmap(spriteMediumBitmap1);
				            	spriteMedium.setWidth(spriteMediumBitmap1.getWidth() / 2);
				            	spriteMedium.setHeight(spriteMediumBitmap1.getHeight() / 2);
				            					            	
				            	spriteBig.setBitmap(spriteBigBitmap1);
			    				spriteBig.setWidth(spriteBigBitmap1.getWidth() / 2);
			    				spriteBig.setHeight(spriteBigBitmap1.getHeight() / 2);
			    			} else
			    			if (currentLevel >= 5 && currentLevel < 10) {
			    				spriteSmall.setBitmap(spriteSmallBitmap2);
				            	spriteSmall.setWidth(spriteSmallBitmap2.getWidth() / 2);
				            	spriteSmall.setHeight(spriteSmallBitmap2.getHeight() / 2);
				            	
				            	spriteMedium.setBitmap(spriteMediumBitmap2);
				            	spriteMedium.setWidth(spriteMediumBitmap2.getWidth() / 2);
				            	spriteMedium.setHeight(spriteMediumBitmap2.getHeight() / 2);
			    				
			    				spriteBig.setBitmap(spriteBigBitmap2);
			    				spriteBig.setWidth(spriteBigBitmap2.getWidth() / 2);
			    				spriteBig.setHeight(spriteBigBitmap2.getHeight() / 2);
			    			}else
			    			if (currentLevel >= 10 && currentLevel < 15) {
			    				spriteSmall.setBitmap(spriteSmallBitmap3);
				            	spriteSmall.setWidth(spriteSmallBitmap3.getWidth() / 2);
				            	spriteSmall.setHeight(spriteSmallBitmap3.getHeight() / 2);
				            	
				            	spriteMedium.setBitmap(spriteMediumBitmap3);
				            	spriteMedium.setWidth(spriteMediumBitmap3.getWidth() / 2);
				            	spriteMedium.setHeight(spriteMediumBitmap3.getHeight() / 2);
			    				
			    				spriteBig.setBitmap(spriteBigBitmap3);
			    				spriteBig.setWidth(spriteBigBitmap3.getWidth() / 2);
			    				spriteBig.setHeight(spriteBigBitmap3.getHeight() / 2);
			    			}else {				    	
			    				spriteSmall.setBitmap(spriteSmallBitmap4);
				            	spriteSmall.setWidth(spriteSmallBitmap4.getWidth() / 2);
				            	spriteSmall.setHeight(spriteSmallBitmap4.getHeight() / 2);
				            	
				            	spriteMedium.setBitmap(spriteMediumBitmap4);
				            	spriteMedium.setWidth(spriteMediumBitmap4.getWidth() / 2);
				            	spriteMedium.setHeight(spriteMediumBitmap4.getHeight() / 2);
			    				
			    				spriteBig.setBitmap(spriteBigBitmap4);
			    				spriteBig.setWidth(spriteBigBitmap4.getWidth() / 2);
			    				spriteBig.setHeight(spriteBigBitmap4.getHeight() / 2);
							}
				            
				            if (progress <= (int)(progressBitmap.getWidth() / 3)) {
				            	fish.setBmp(fishSmallBitmap);
				            	fish.setWidth(fishSmallBitmap.getWidth() / 2);
				            	fish.setHeight(fishSmallBitmap.getHeight() / 2);
								if (fish.collidesWith(spriteMedium) && isNoDeath == false) {
									if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
										SoundManager.playSound(SOUND_DIE, SOUND_SPEED);										
									}
									mTimer.start();
					            	timer.start();
									canMove = false;
					            	int x = fish.getX() - (bmpTemp.getWidth() / 6);
					            	int y = fish.getY() - (bmpTemp.getHeight() / 6);
				            		fishes.remove(fish);
				            		life = life - 1;
				            		fishes.add(new Fish(this, fishSmallBitmap));
					            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));						            	
								} else if (fish.collidesWith(spriteBig) && isNoDeath == false) {
									if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
										SoundManager.playSound(SOUND_DIE, SOUND_SPEED);
									}
									mTimer.start();
					            	timer.start();
									canMove = false;
					            	int x = fish.getX() - (bmpTemp.getWidth() / 6);
					            	int y = fish.getY() - (bmpTemp.getHeight() / 6);
				            		fishes.remove(fish);
				            		life = life - 1;
				            		fishes.add(new Fish(this, fishSmallBitmap));
					            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));	
								}
								if (fish.collidesWith(spriteSmall)) {
									int x = spriteSmall.getX() - (bmpTemp.getWidth() / 6);
					            	int y = spriteSmall.getY() - (bmpTemp.getHeight() / 6);
					            	int x1 = spriteSmall.getX() - (bmpTempLevelUp.getWidth() / 2);
					            	int y1 = spriteSmall.getY() - (bmpTempLevelUp.getHeight() / 2);
					            	if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
					            		SoundManager.playSound(SOUND_EAT, SOUND_SPEED);
									}					            	
					            	spritesSmall.remove(spriteSmall);
					            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
					            	spritesSmall.add(createSprite(R.drawable.f12));		            	
					            	score = score + 10;
					            	progress = progress + (int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS);	
					            	if (progress >= (int)(progressBitmap.getWidth() / 3) 
					            			&& progress <= (int)(progressBitmap.getWidth() / 3) 
					            			+ (int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS)) {
										tempLevelUps.add(new TempLevelUp(tempLevelUps, this, x1, y1, bmpTempLevelUp));
										if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
						            		SoundManager.playSound(SOUND_LEVEL_UP, SOUND_SPEED);
										}
									}
								} 
							} else
				            if (progress > (int)(progressBitmap.getWidth() / 3) && progress <= (int)((progressBitmap.getWidth() / 3))*2) {	
				            	fish.setBmp(fishMediumBitmap);
				            	fish.setWidth(fishMediumBitmap.getWidth() / 2);
				            	fish.setHeight(fishMediumBitmap.getHeight() / 2);
				            	if (fish.collidesWith(spriteSmall)) {
									int x = spriteSmall.getX() - (bmpTemp.getWidth() / 6);
					            	int y = spriteSmall.getY() - (bmpTemp.getHeight() / 6);
					            	int x1 = spriteSmall.getX() - (bmpTempLevelUp.getWidth() / 2);
					            	int y1 = spriteSmall.getY() - (bmpTempLevelUp.getHeight() / 2);
					            	if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
					            		SoundManager.playSound(SOUND_EAT, SOUND_SPEED);					            		
									}
					            	spritesSmall.remove(spriteSmall);
					            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
					            	spritesSmall.add(createSprite(R.drawable.f12));		            	
					            	score = score + 10;
					            	progress = progress + (int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS);
					            	if (progress >= ((int)((progressBitmap.getWidth() / 3)*2) + (int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS)) 
					            			&& progress < ((int)(progressBitmap.getWidth() / 3)*2) 
					            			+ (((int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS)) * 2)) {
										tempLevelUps.add(new TempLevelUp(tempLevelUps, this, x1, y1, bmpTempLevelUp));
									}
								} else if (fish.collidesWith(spriteMedium)) {
									int x = spriteMedium.getX() - (bmpTemp.getWidth() / 6);
					            	int y = spriteMedium.getY() - (bmpTemp.getHeight() / 6);
					            	int x1 = spriteMedium.getX() - (bmpTempLevelUp.getWidth() / 2);
					            	int y1 = spriteMedium.getY() - (bmpTempLevelUp.getHeight() / 2);
					            	if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
					            		SoundManager.playSound(SOUND_EAT, SOUND_SPEED);
									}
					            	spritesMedium.remove(spriteMedium);
					            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
					            	spritesMedium.add(createSprite(R.drawable.f14));		            	
					            	score = score + 20;
					            	progress = progress + (int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS);
					            	if (progress > (int)((progressBitmap.getWidth() / 3)*2) 
					            			&& progress <= (int)((progressBitmap.getWidth() / 3)*2) 
					            			+ (int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS)) {
										tempLevelUps.add(new TempLevelUp(tempLevelUps, this, x1, y1, bmpTempLevelUp));
										if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
						            		SoundManager.playSound(SOUND_LEVEL_UP, SOUND_SPEED);
										}
									}
								} else
								if (fish.collidesWith(spriteBig) && isNoDeath == false) {
									if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
										SoundManager.playSound(SOUND_DIE, SOUND_SPEED);
									}
									mTimer.start();
					            	timer.start();
									canMove = false;
					            	int x = fish.getX() - (bmpTemp.getWidth() / 6);
					            	int y = fish.getY() - (bmpTemp.getHeight() / 6);
					            	fishes.remove(fish);
					            	life = life - 1;
					            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
					            	fishes.add(new Fish(this, fishMediumBitmap));						            	
								} 
							} else
							if (progress > (int)((progressBitmap.getWidth() / 3))*2) {		
								fish.setBmp(fishBigBitmap);
				            	fish.setWidth(fishBigBitmap.getWidth() / 2);
				            	fish.setHeight(fishBigBitmap.getHeight() / 2);
				            	
								if (fish.collidesWith(spriteSmall)) {
									int x = spriteSmall.getX() - (bmpTemp.getWidth() / 6);
					            	int y = spriteSmall.getY() - (bmpTemp.getHeight() / 6);
					            	if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
					            		SoundManager.playSound(SOUND_EAT, SOUND_SPEED);
									}
					            	spritesSmall.remove(spriteSmall);
					            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
					            	spritesSmall.add(createSprite(R.drawable.f12));		            	
					            	score = score + 10;
					            	progress = progress + (int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS);
								} else if (fish.collidesWith(spriteMedium)) {
									int x = spriteMedium.getX() - (bmpTemp.getWidth() / 6);
					            	int y = spriteMedium.getY() - (bmpTemp.getHeight() / 6);
					            	if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
					            		SoundManager.playSound(SOUND_EAT, SOUND_SPEED);
									}
					            	spritesMedium.remove(spriteMedium);
					            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
					            	spritesMedium.add(createSprite(R.drawable.f14));		            	
					            	score = score + 20;
					            	progress = progress + (int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS);
								} else if (fish.collidesWith(spriteBig)) {
									int x = spriteBig.getX() - (bmpTemp.getWidth() / 6);
					            	int y = spriteBig.getY() - (bmpTemp.getHeight() / 6);
					            	if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
					            		SoundManager.playSound(SOUND_EAT, SOUND_SPEED);
									}
					            	spritesBig.remove(spriteBig);
					            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
					            	spritesBig.add(createSprite(R.drawable.f5));		            	
					            	score = score + 30;
					            	progress = progress + (int) (progressBitmap.getWidth() / MyProgressBar.BMP_COLUMNS);
								}
							}
						}
					}
				}
			}
			for (int l = suaBiens.size()-1 ; l >= 0  ; l--) {					
	            suaBien = suaBiens.get(l);
	            if (fish.collidesWith(suaBien) && isNoDeath == false) {
	            	if (sPreferences.getBoolean(KEY_SOUND, true) == true) {
						SoundManager.playSound(SOUND_DIE, SOUND_SPEED);
					}
	            	mTimer.start();
	            	timer.start();
					canMove = false;
	            	int x = fish.getX() - (bmpTemp.getWidth() / 6);
	            	int y = fish.getY() - (bmpTemp.getHeight() / 6);
	            	fishes.remove(fish);
	            	life = life - 1;
	            	tempSprites.add(new TempSprite(tempSprites, this, x, y, bmpTemp));
	            	if (progress <= (int)(progressBitmap.getWidth() / 3)) {
	            		fishes.add(new Fish(this, fishSmallBitmap));
					} else if (progress > (int)(progressBitmap.getWidth() / 3) && progress <= (int)((progressBitmap.getWidth() / 3))*2) {
						fishes.add(new Fish(this, fishMediumBitmap));
					} else {
						fishes.add(new Fish(this, fishBigBitmap));
					}	            	
				}
			}
			if(issuaheart){//Kiem tra va cham voi trai tim
				if (fish.collidesWith(suaHeart) && isNoDeath == false) {
					life ++;
					issuaheart=false;
				}
			}
		}
		
		
		
		if (canMove == true) {
			fish.move(eventX, eventY);			
		}
	}
	
	public void updatePauseMenu() {
		isMusicOn = sPreferences.getBoolean(KEY_MUSIC, true);
		isSoundOn = sPreferences.getBoolean(KEY_SOUND, true);
		if (isMusicOn == true) {						
			btMusic.setBmp(buttonMusicBitmap);
		} else {
			btMusic.setBmp(buttonOnMusicBitmap);
		}				
		if (isSoundOn == true) {						
			btSound.setBmp(buttonSoundBitmap);
		} else {				
			btSound.setBmp(buttonOnSoundBitmap);
		}
//		if (sPreferences.getBoolean(KEY_MUSIC, true) == false) {
//			SoundManager.cleanup();
//		}
	}
	
	public void updateMainMenu() {
		isMusicOn = sPreferences.getBoolean(KEY_MUSIC, true);
		isSoundOn = sPreferences.getBoolean(KEY_SOUND, true);
		if (isMusicOn == true) {						
			btMusicMain.setBmp(buttonMusicBitmap);
		} else {				
			btMusicMain.setBmp(buttonOnMusicBitmap);
		}
		
		if (isSoundOn == true) {						
			btSoundMain.setBmp(buttonSoundBitmap);
		} else {				
			btSoundMain.setBmp(buttonOnSoundBitmap);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		int l1 = btPause.getX();
		int w1 = btPause.getWidth();
		int t1 = btPause.getY();
		int h1 = btPause.getHeight();
		
		int l2 = btResume.getX();
		int w2 = btResume.getWidth();
		int t2 = btResume.getY();
		int h2 = btResume.getHeight();
		
		int l3 = btMenu.getX();
		int w3 = btMenu.getWidth();
		int t3 = btMenu.getY();
		int h3 = btMenu.getHeight();
		
		int l4 = btNext.getX();
		int w4 = btNext.getWidth();
		int t4 = btNext.getY();
		int h4 = btNext.getHeight();
		
		int l5 = btRestart.getX();
		int w5 = btRestart.getWidth();
		int t5 = btRestart.getY();
		int h5 = btRestart.getHeight();
		
		if (gameThread.currentState == GameThread.STATE_RUNNING) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				fish.handleActionDown((int)event.getX(), (int)event.getY());			
				if ((event.getX() >= l1) && (event.getX() <= (l1 + w1))) {
					if ((event.getY() >= t1) && (event.getY() <= (t1 + h1))) {
						btPause.setBmp(buttonUnPauseBitmap);
						gameThread.setState(GameThread.STATE_PAUSE);
						return true;
					}
				}	
			}
		
			if (event.getAction() == MotionEvent.ACTION_MOVE) {
				if (gameThread.currentState == GameThread.STATE_RUNNING) {
					if (fish.isTouched()) {
						canMove = true;
						eventX = (int) event.getX();
						eventY = (int) event.getY();
						return true;
					}
				}
			}
													
			if (event.getAction() == MotionEvent.ACTION_UP) {
				if (gameThread.currentState == GameThread.STATE_RUNNING) {
					if (fish.isTouched()) {
						canMove = true;
						eventX = (int) event.getX();
						eventY = (int) event.getY();
						return true;
					}	
				}
			}
			return true;
		}
			
		if (gameThread.currentState == GameThread.STATE_PAUSE) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				btMusic.handleActionDown((int)event.getX(), (int)event.getY());
				btSound.handleActionDown((int)event.getX(), (int)event.getY());
				return true;
			}
			if (event.getAction() == MotionEvent.ACTION_UP) {
				if ((event.getX() >= l2) && (event.getX() <= (l2 + w2))) {
					if ((event.getY() >= t2) && (event.getY() <= (t2 + h2))) {						
						btPause.setBmp(buttonPauseBitmap);
						gameThread.setState(GameThread.STATE_RUNNING);
						return true;
					}
				}
				
				if ((event.getX() >= l3) && (event.getX() <= (l3 + w3))) {
					if ((event.getY() >= t3) && (event.getY() <= (t3 + h3))) {
						gameThread.resetGame();
						gameThread.setState(GameThread.STATE_SHOW_MENU);
						return true;
					}
				}
				
				if (btMusic.isTouched()) {
					isMusicOn = sPreferences.getBoolean(KEY_MUSIC, true);
					if (isMusicOn == true) {						
						setMusicOn(false);
						editor.putBoolean(KEY_MUSIC, false);
						editor.commit();	
						if (MainActivity.player != null && MainActivity.player.isPlaying()) {
							MainActivity.player.pause();
						}
					} else {						
						setMusicOn(true);
						editor.putBoolean(KEY_MUSIC, true);
						editor.commit();
						if (MainActivity.player != null) {
							MainActivity.player.seekTo(0);
							MainActivity.player.start();
						}
					}					
				}
				
				if (btSound.isTouched()) {
					isSoundOn = sPreferences.getBoolean(KEY_SOUND, true);
					if (isSoundOn == true) {			
						setSoundOn(false);
						editor.putBoolean(KEY_SOUND, false);
						editor.commit();						
					} else {
						setSoundOn(true);
						editor.putBoolean(KEY_SOUND, true);
						editor.commit();
					}
				}
				return true;
			}
		}
		
		if (gameThread.currentState == GameThread.STATE_SHOW_LEVEL) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				for (int i = 0; i < levelSelectSprite.length; i++) {
					levelSelectSprite[i].handleActionTouch((int)event.getX(), (int)event.getY());
				}
				return true;
			}
			
			if (event.getAction() == MotionEvent.ACTION_UP) {
				if (levelSelectSprite[0].isTouch()) {
					if (MainActivity.player != null && sPreferences.getBoolean(KEY_MUSIC, true) == true) {
						MainActivity.player.seekTo(0);
						MainActivity.player.start();
					}
					btPause.setBmp(buttonPauseBitmap);
					currentLevel = 1;
					MyProgressBar.BMP_COLUMNS=MyProgressBar.BMP_COLUMNS+currentLevel;
					gameThread.setState(GameThread.STATE_RUNNING);
					removeSuaBiens();
					createSuaBiens();
					mTimer.start();
					timer.start();
				}
				for (int i = 1; i < levelSelectSprite.length; i++) {
					if (levelSelectSprite[i].isTouch() && levelSelectSprite[i].isUnlock()) {
						if (MainActivity.player != null && sPreferences.getBoolean(KEY_MUSIC, true) == true) {
							MainActivity.player.seekTo(0);
							MainActivity.player.start();
						}
						btPause.setBmp(buttonPauseBitmap);
						currentLevel = i + 1;
						MyProgressBar.BMP_COLUMNS=MyProgressBar.BMP_COLUMNS+currentLevel;
						gameThread.setState(GameThread.STATE_RUNNING);
						removeSuaBiens();
						createSuaBiens();
						mTimer.start();
						timer.start();
					}
				}				
				return true;
			}
		}
			
		if(gameThread.currentState == GameThread.STATE_WIN){
			if (event.getAction() == MotionEvent.ACTION_DOWN) {	
				return true;
			}
			if (event.getAction() == MotionEvent.ACTION_UP) {
				if ((event.getX() >= l3) && (event.getX() <= (l3 + w3))) {
					if ((event.getY() >= t3) && (event.getY() <= (t3 + h3))) {
						gameThread.resetGame();
						numLevelCompleted = sPreferences.getInt(KEY_NUMBER_LEVEL, 0);
						if (currentLevel - 1 == numLevelCompleted) {					
							numLevelCompleted = numLevelCompleted + 1;
							levelSelectSprite[numLevelCompleted].setUnlock(true);
							levelSelectSprite[numLevelCompleted].setLevelSelcetBitmap(levelOpenBitmap);
							editor.putInt(KEY_NUMBER_LEVEL, numLevelCompleted);
							editor.commit();
						}
						gameThread.setState(GameThread.STATE_SHOW_MENU);
						for (int i = 0; i <= numLevelCompleted; i++) {
							levelSelectSprite[i].setLevelSelcetBitmap(levelOpenBitmap);
							levelSelectSprite[i].setUnlock(true);
						}
						return true;
					}
				}
				
				if ((event.getX() >= l4) && (event.getX() <= (l4 + w4))) {
					if ((event.getY() >= t4) && (event.getY() <= (t4 + h4))) {							
						if (currentLevel < 20) {
							btPause.setBmp(buttonPauseBitmap);
							gameThread.resetToNextLevel();
							numLevelCompleted = sPreferences.getInt(KEY_NUMBER_LEVEL, 0);
							if(currentLevel - 1 == numLevelCompleted){							
								numLevelCompleted = numLevelCompleted + 1;
								levelSelectSprite[numLevelCompleted].setUnlock(true);
								levelSelectSprite[numLevelCompleted].setLevelSelcetBitmap(levelOpenBitmap);
								editor.putInt(KEY_NUMBER_LEVEL, numLevelCompleted);
								editor.commit();
							}
							currentLevel = currentLevel + 1;
							MyProgressBar.BMP_COLUMNS=MyProgressBar.BMP_COLUMNS+currentLevel;
							//Len level
							countheart=2;
							issuaheart=false;
							if(life>5){
								life=5;
							}
							
							gameThread.setState(GameThread.STATE_RUNNING);
							removeSuaBiens();
							createSuaBiens();
							mTimer.start();
							timer.start();
							return true;
						} else {
							gameThread.setState(GameThread.STATE_SHOW_MENU);
							return true;
						}
					}
				}
				return true;
			}
			
		}
		if (gameThread.currentState == GameThread.STATE_LOSE) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				return true;
			}
			if (event.getAction() == MotionEvent.ACTION_UP) {
				if ((event.getX() >= l5) && (event.getX() <= (l5 + w5))) {
					if ((event.getY() >= t5) && (event.getY() <= (t5 + h5))) {
						btPause.setBmp(buttonPauseBitmap);
						gameThread.resetGame();
						gameThread.setState(GameThread.STATE_RUNNING);
						removeSuaBiens();
						createSuaBiens();
						mTimer.start();
						timer.start();
						return true;
					}
				}
				
				if ((event.getX() >= l3) && (event.getX() <= (l3 + w3))) {
					if ((event.getY() >= t3) && (event.getY() <= (t3 + h3))) {
						gameThread.resetGame();
						gameThread.setState(GameThread.STATE_SHOW_MENU);
						return true;
					}
				}					
				
			}			
		}
		
		if (gameThread.currentState == GameThread.STATE_HELP) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				btBack.handleActionDown((int)event.getX(), (int)event.getY());
				return true;
			}
			if (event.getAction() == MotionEvent.ACTION_UP) {
				if (btBack.isTouched()) {
					gameThread.setState(GameThread.STATE_SHOW_MENU);
					return true;
				}				
			}			
		}
		return false;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}

	public int getMoveEvent() {
		return moveEvent;
	}

	public void setMoveEvent(int moveEvent) {
		this.moveEvent = moveEvent;
	}

	public GameThread getGameThread() {
		return gameThread;
	}

	public void setGameThread(GameThread gameThread) {
		this.gameThread = gameThread;
	}


	public int getLife() {
		return life;
	}


	public void setLife(int life) {
		this.life = life;
	}

	public int getProgress() {
		return progress;
	}


	public void setProgress(int progress) {
		this.progress = progress;
	}

	public boolean isEnterGame() {
		return enterGame;
	}


	public void setEnterGame(boolean enterGame) {
		this.enterGame = enterGame;
	}

	public MySprite getBtPlay() {
		return btPlay;
	}

	public void setBtPlay(MySprite btPlay) {
		this.btPlay = btPlay;
	}

	public MySprite getBtExit() {
		return btExit;
	}

	public void setBtExit(MySprite btExit) {
		this.btExit = btExit;
	}

	public MySprite getBtHelp() {
		return btHelp;
	}

	public void setBtHelp(MySprite btHelp) {
		this.btHelp = btHelp;
	}

	public MySprite getBtPause() {
		return btPause;
	}

	public void setBtPause(MySprite btPause) {
		this.btPause = btPause;
	}
	
	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public Fish getFish() {
		return fish;
	}

	public void setFish(Fish fish) {
		this.fish = fish;
	}

	public Bitmap getFishSmallBitmap() {
		return fishSmallBitmap;
	}

	public void setFishSmallBitmap(Bitmap fishSmallBitmap) {
		this.fishSmallBitmap = fishSmallBitmap;
	}

	public boolean isCanMove() {
		return canMove;
	}

	public void setCanMove(boolean canMove) {
		this.canMove = canMove;
	}

	public int getaX() {
		return aX;
	}

	public void setaX(int aX) {
		this.aX = aX;
	}

	public int getaY() {
		return aY;
	}

	public void setaY(int aY) {
		this.aY = aY;
	}

	public int getaZ() {
		return aZ;
	}

	public void setaZ(int aZ) {
		this.aZ = aZ;
	}

	public float getScaleY() {
		return scaleY;
	}

	public void setScaleY(float scaleY) {
		this.scaleY = scaleY;
	}

	public float getScaleX() {
		return scaleX;
	}

	public void setScaleX(float scaleX) {
		this.scaleX = scaleX;
	}

	public LevelSelectSprite[] getLevelSelectSprite() {
		return levelSelectSprite;
	}

	public void setLevelSelectSprite(LevelSelectSprite[] levelSelectSprite) {
		this.levelSelectSprite = levelSelectSprite;
	}

	public Bitmap getLevelOpenBitmap() {
		return levelOpenBitmap;
	}

	public void setLevelOpenBitmap(Bitmap levelOpenBitmap) {
		this.levelOpenBitmap = levelOpenBitmap;
	}

	public int getNumLevelCompleted() {
		return numLevelCompleted;
	}

	public void setNumLevelCompleted(int numLevelCompleted) {
		this.numLevelCompleted = numLevelCompleted;
	}

	public boolean isNoDeath() {
		return isNoDeath;
	}

	public void setNoDeath(boolean isNoDeath) {
		this.isNoDeath = isNoDeath;
	}

	public Bitmap getButtonOnMusicBitmap() {
		return buttonOnMusicBitmap;
	}

	public void setButtonOnMusicBitmap(Bitmap buttonOnMusicBitmap) {
		this.buttonOnMusicBitmap = buttonOnMusicBitmap;
	}

	public Bitmap getButtonOnSoundBitmap() {
		return buttonOnSoundBitmap;
	}

	public void setButtonOnSoundBitmap(Bitmap buttonOnSoundBitmap) {
		this.buttonOnSoundBitmap = buttonOnSoundBitmap;
	}

	public boolean isMusicOn() {
		return isMusicOn;
	}

	public void setMusicOn(boolean isMusicOn) {
		this.isMusicOn = isMusicOn;
	}

	public boolean isSoundOn() {
		return isSoundOn;
	}

	public void setSoundOn(boolean isSoundOn) {
		this.isSoundOn = isSoundOn;
	}

	public MySprite getBtMusicMain() {
		return btMusicMain;
	}

	public void setBtMusicMain(MySprite btMusicMain) {
		this.btMusicMain = btMusicMain;
	}

	public MySprite getBtSoundMain() {
		return btSoundMain;
	}

	public void setBtSoundMain(MySprite btSoundMain) {
		this.btSoundMain = btSoundMain;
	}

	public Bitmap getButtonMusicBitmap() {
		return buttonMusicBitmap;
	}

	public void setButtonMusicBitmap(Bitmap buttonMusicBitmap) {
		this.buttonMusicBitmap = buttonMusicBitmap;
	}

	public Bitmap getButtonSoundBitmap() {
		return buttonSoundBitmap;
	}

	public void setButtonSoundBitmap(Bitmap buttonSoundBitmap) {
		this.buttonSoundBitmap = buttonSoundBitmap;
	}

	public boolean isSetAlpha() {
		return isSetAlpha;
	}

	public void setSetAlpha(boolean isSetAlpha) {
		this.isSetAlpha = isSetAlpha;
	}

	public Bitmap getStarLightBitmap() {
		return starLightBitmap;
	}

	public void setStarLightBitmap(Bitmap starLightBitmap) {
		this.starLightBitmap = starLightBitmap;
	}

	public Sprite getSpriteBig() {
		return spriteBig;
	}

	public void setSpriteBig(Sprite spriteBig) {
		this.spriteBig = spriteBig;
	}

	public Sprite getSpriteMedium() {
		return spriteMedium;
	}

	public void setSpriteMedium(Sprite spriteMedium) {
		this.spriteMedium = spriteMedium;
	}

	public Sprite getSpriteSmall() {
		return spriteSmall;
	}

	public void setSpriteSmall(Sprite spriteSmall) {
		this.spriteSmall = spriteSmall;
	}

	public List<Sprite> getSpritesSmall() {
		return spritesSmall;
	}

	public void setSpritesSmall(List<Sprite> spritesSmall) {
		this.spritesSmall = spritesSmall;
	}

	public List<Sprite> getSpritesMedium() {
		return spritesMedium;
	}

	public void setSpritesMedium(List<Sprite> spritesMedium) {
		this.spritesMedium = spritesMedium;
	}

	public List<Sprite> getSpritesBig() {
		return spritesBig;
	}

	public void setSpritesBig(List<Sprite> spritesBig) {
		this.spritesBig = spritesBig;
	}
}
